package game_logic.control.listener;

import connect.Client;
import game_logic.Controller;
import game_logic.model.Board;

import javax.swing.*;
import java.util.Objects;

public class AcceptThread extends Thread{
    public void run(){
        while(true){
            try {
                listener.client.receiveMessage();
                String getCord = listener.client.getReceiveMsg();
                if (Objects.equals(getCord, "HdlCblCcqCjw")) return;
                int sx, sy, tx, ty;
                String[] change_position = getCord.substring(getCord.lastIndexOf(':') + 2, getCord.length()).split(" ");
                sx = Integer.valueOf(change_position[0]);
                sy = Integer.valueOf(change_position[1]);
                tx = Integer.valueOf(change_position[2]);
                ty = Integer.valueOf(change_position[3]);
                if (!controller.move(sx, sy, tx, ty)) return; // 如果移动失败，那么结束
                if (this.board.isGameOver()) {
                    // Client.release();
                    return;
                }
                listener.currentPlayer = (1+ listener.currentPlayer)%2;
            } catch (Exception ex) {
                System.out.println("接收信息失败");
                throw new RuntimeException(ex);
            }
        }
    }
    public AcceptThread(Listener listener, Controller controller, Board board){
        this.listener=listener;
        this.controller=controller;
        this.board = board;
    }
    private Board board;
    private Listener listener;
    private Controller controller;

}
